package com.biboheart.adapter.compiler.lexical;

import com.biboheart.adapter.compiler.enums.TokenState;
import com.biboheart.adapter.compiler.enums.TokenType;
import com.biboheart.adapter.compiler.token.Token;
import com.biboheart.adapter.support.enums.DelimitEnum;

final class TokenizerDelimiters implements Tokenizer {
    private TokenBuffer tokenBuffer;
    private TokenState state;

    @Override
    public TokenState entry(char ch, int offset, String content) {
        state = TokenState.STATE_FAIL;
        if (!DelimitEnum.contains(String.valueOf(ch))) {
            return state;
        }
        tokenBuffer = new TokenBuffer(offset);
        tokenBuffer.push(ch);
        state = TokenState.STATE_END;
        return TokenState.STATE_END;
    }

    @Override
    public TokenState getState() {
        return state;
    }

    @Override
    public TokenState analyse(char ch, int offset, String content) {
        return TokenState.STATE_END_BACK;
    }

    @Override
    public Token generateToken() {
        if (!TokenState.STATE_END.equals(state) && !TokenState.STATE_END_BACK.equals(state)) {
            return null;
        }
        return new Token(tokenBuffer.getStart(), tokenBuffer.getEnd(),
                TokenType.TOKEN_DELIMITERS, tokenBuffer.buffer(),
                tokenBuffer.value(TokenType.TOKEN_DELIMITERS), tokenBuffer.buffer());
    }
}
